home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE12 / ENUMFNTF.C < prev    next >
C/C++ Source or Header  |  1996-01-30  |  8KB  |  249 lines

  1.  
  2. #include <windows.h>  
  3. #include "EnumFntF.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. int CALLBACK EnumFontProc( ENUMLOGFONT* lpelf, NEWTEXTMETRIC* lpntm, 
  108.                            int nFontType, LPARAM lParam )
  109. {
  110.  
  111.    // Load only TrueType fonts into the list box.
  112.    //............................................
  113.    if ( nFontType & TRUETYPE_FONTTYPE )
  114.    {
  115.       if ( IsWindow( (HWND)lParam ) )
  116.       {
  117.          SendMessage( (HWND)lParam, LB_ADDSTRING, 0, 
  118.                       (LPARAM)lpelf->elfLogFont.lfFaceName );
  119.       }
  120.    }
  121.  
  122.    return( TRUE );
  123. }
  124.  
  125.  
  126. int CALLBACK FindFontProc( ENUMLOGFONT* lpelf, NEWTEXTMETRIC* lpntm, 
  127.                            int nFontType, LPARAM lParam )
  128. {
  129.    // Find the regular font.
  130.    //.......................
  131.    if ( strcmp( lpelf->elfStyle, "Regular" ) == 0 )
  132.       memcpy( (LOGFONT*)lParam, &lpelf->elfLogFont, sizeof( LOGFONT ) );
  133.  
  134.    return( TRUE );
  135. }
  136.  
  137.  
  138. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  139. {
  140. static HWND hList;
  141.  
  142.    switch( uMsg )
  143.    {
  144.       case WM_CREATE  :
  145.               {
  146.                  HDC hDC;
  147.  
  148.                  // Create a list box and fill it with the
  149.                  // available TrueType font names.
  150.                  //.......................................              
  151.                  hList = CreateWindow( "LISTBOX", "", 
  152.                                     WS_CHILD | WS_VISIBLE | LBS_STANDARD,
  153.                                     0, 0, 200, 200, hWnd, (HMENU)101, hInst,
  154.                                     NULL );
  155.  
  156.                  hDC = GetDC( hWnd );
  157.                  EnumFontFamilies( hDC, NULL, EnumFontProc,(LPARAM)hList );
  158.                  ReleaseDC( hWnd, hDC );
  159.  
  160.                  SendMessage( hList, LB_SETCURSEL, 0, 0 );
  161.               }
  162.               break;
  163.  
  164.       case WM_COMMAND :
  165.               switch( LOWORD( wParam ) )
  166.               {
  167.                  case IDM_TEST :
  168.                         {
  169.                            HDC     hDC;
  170.                            LOGFONT lf;
  171.                            HFONT   hFont;
  172.                            char    szFaceName[128];
  173.                            int     nSel;
  174.  
  175.                            // Retrieve the currently selected font name.
  176.                            //...........................................
  177.                            nSel = SendMessage( hList, LB_GETCURSEL, 0, 0 );
  178.                            SendMessage( hList, LB_GETTEXT, nSel,
  179.                                         (LPARAM)szFaceName );
  180.  
  181.                            // Repaint the client area of the window.
  182.                            //.......................................
  183.                            InvalidateRect( hWnd, NULL, TRUE );
  184.                            UpdateWindow( hWnd );
  185.  
  186.                            // Find the selected font and retrieve the
  187.                            // LOGFONT structure for the font.
  188.                            //.........................................
  189.                            hDC = GetDC( hWnd );
  190.                            EnumFontFamilies( hDC, szFaceName,
  191.                                              FindFontProc,(LPARAM) &lf );
  192.  
  193.                            // Create the font and output a sample text string.
  194.                            //.................................................
  195.                            hFont = CreateFontIndirect( &lf );
  196.                            SelectObject( hDC, hFont );
  197.  
  198.                            TextOut( hDC, 10, 230, "Sample Text Output.", 19 );
  199.  
  200.                            ReleaseDC( hWnd, hDC );
  201.                            DeleteObject( hFont );
  202.                         }
  203.                         break;
  204.  
  205.                  case IDM_ABOUT :
  206.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  207.                         break;
  208.  
  209.                  case IDM_EXIT :
  210.                         DestroyWindow( hWnd );
  211.                         break;
  212.               }
  213.               break;
  214.       
  215.       case WM_DESTROY :
  216.               PostQuitMessage(0);
  217.               break;
  218.  
  219.       default :
  220.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  221.    }
  222.  
  223.    return( 0L );
  224. }
  225.  
  226.  
  227. LRESULT CALLBACK About( HWND hDlg,           
  228.                         UINT message,        
  229.                         WPARAM wParam,       
  230.                         LPARAM lParam)
  231. {
  232.    switch (message) 
  233.    {
  234.        case WM_INITDIALOG: 
  235.                return (TRUE);
  236.  
  237.        case WM_COMMAND:                              
  238.                if (   LOWORD(wParam) == IDOK         
  239.                    || LOWORD(wParam) == IDCANCEL)    
  240.                {
  241.                        EndDialog(hDlg, TRUE);        
  242.                        return (TRUE);
  243.                }
  244.                break;
  245.    }
  246.  
  247.    return (FALSE); 
  248. }
  249.